package main

import "math"

func reverseBytes(s []byte) []byte {
	for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
		s[i], s[j] = s[j], s[i]
	}
	return s
}

func gcd(a int, b int) int {
	if a < b {
		a, b = b, a
	}
	for b != 0 {
		temp := a % b
		a = b
		b = temp
	}
	return a
}

type ListNode struct {
	Val  int
	Next *ListNode
}

func max(a int, b int) int {
	if a > b {
		return a
	}
	return b
}

func min(a int, b int) int {
	if a < b {
		return a
	}
	return b
}

func binarySearch(nums []int, target int) int {
	left := 0
	right := len(nums) - 1
	mid := 0
	for left <= right {
		mid = left + (right-left)/2
		if nums[mid] == target {
			return mid
		} else if nums[mid] > target {
			right = mid - 1
		} else {
			left = mid + 1
		}
	}
	return 0
}
func solveQueries(nums []int, queries []int) []int {
	map1 := map[int][]int{}
	for i, num := range nums {
		map1[num] = append(map1[num], i)
	}
	ans := make([]int, len(queries))
	for i, query := range queries {
		list1, _ := map1[nums[query]]
		if len(list1) == 1 {
			ans[i] = -1
		} else {
			index := binarySearch(list1, query)
			minDis := math.MaxInt
			if index > 0 {
				pre := index - 1
				minDis = min(minDis, list1[index]-list1[pre])
			} else {
				pre := len(list1) - 1
				minDis = min(minDis, list1[index]+len(nums)-list1[pre])
			}
			if index < len(list1)-1 {
				next := index + 1
				minDis = min(minDis, list1[next]-list1[index])
			} else {
				next := 0
				minDis = min(minDis, list1[next]+len(nums)-list1[index])
			}
			ans[i] = minDis
		}
	}
	return ans
}
func main() {
	nums := []int{1, 3, 1, 4, 1, 3, 2}
	queries := []int{0, 3, 5}
	solveQueries(nums, queries)
}
